home *** CD-ROM | disk | FTP | other *** search
- unit GridEgU;
-
- interface
-
- uses
- WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- Db, DBTables, Grids;
-
- type
- TForm1 = class(TForm)
- Grid: TStringGrid;
- tblCustomer: TTable;
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- type
- TRecordDesc = class(TObject)
- public
- CustNo,
- State: String;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- var
- List: TStrings;
- RecordDesc: TRecordDesc;
- Loop: Integer;
- begin
- List := TStringList.Create;
- try
- tblCustomer.Open;
- { Loop through table records }
- while not tblCustomer.Eof do
- begin
- { Create new object instance }
- RecordDesc := TRecordDesc.Create;
- try
- { Set up object data fields }
- RecordDesc.CustNo := tblCustomer.FieldByName('CustNo').AsString;
- RecordDesc.State := tblCustomer.FieldByName('State').AsString;
- { Let string list look after object for a while }
- List.AddObject(tblCustomer.FieldByName('Company').AsString, RecordDesc);
- except
- RecordDesc.Free;
- end;
- tblCustomer.Next
- end;
- tblCustomer.Close;
- { Initialise grid }
- Grid.RowCount := List.Count + 1;
- Grid.ColCount := 3;
- Grid.Cells[0, 0] := 'Customer No.';
- Grid.Cells[1, 0] := 'Company';
- Grid.Cells[2, 0] := 'State';
- { Loop through string list setting up grid rows }
- for Loop := 0 to List.Count - 1 do
- begin
- Grid.Cells[0, Loop + 1] := TRecordDesc(List.Objects[Loop]).CustNo;
- Grid.Cells[1, Loop + 1] := List[Loop];
- Grid.Cells[2, Loop + 1] := TRecordDesc(List.Objects[Loop]).State;
- end;
- finally
- { Delete objects stored in list }
- for Loop := 0 to List.Count - 1 do
- List.Objects[Loop].Free;
- { Delete list }
- List.Free
- end
- end;
-
- end.
-